package com.ruicar.afs.cloud.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.product.condition.LprCondition;
import com.ruicar.afs.cloud.product.entity.LprConfiguration;
import com.ruicar.afs.cloud.product.entity.ProductPlanRate;
import com.ruicar.afs.cloud.product.enums.StatusEnum;
import com.ruicar.afs.cloud.product.service.LprConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * @author xudongdong
 * @date 2020/04/23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/lpr")
@Api(value = "lpr", description = "LPR配置信息")
public class LprConfigurationController {

    private final LprConfigurationService lprService;

    @PostMapping("/getLprListData")
    @ApiOperation(value = "多条件分页获取LPR配置数据")
    public IResponse<List<LprConfiguration>> listLprConfiguration(@RequestBody QueryCondition<LprCondition> lpr) {
        String startLprDate = null;
        String endLprDate = null;
        String startEffectiveDate = null;
        String endEffectiveDate = null;
        if (lpr.getCondition().getLprDates() != null && lpr.getCondition().getLprDates().length > 0) {
            startLprDate = lpr.getCondition().getLprDates()[0];
            endLprDate = lpr.getCondition().getLprDates()[1];
        }
        if (lpr.getCondition().getSystemEffectiveTimes() != null && lpr.getCondition().getSystemEffectiveTimes().length > 0) {
            startEffectiveDate = lpr.getCondition().getSystemEffectiveTimes()[0];
            endEffectiveDate = lpr.getCondition().getSystemEffectiveTimes()[1];
        }
        return IResponse.success(lprService.page(new Page(lpr.getPageNumber(), lpr.getPageSize()), Wrappers.<LprConfiguration>query().lambda()
                .ge(StringUtils.isNotEmpty(startLprDate), LprConfiguration::getLprDate, startLprDate)
                .le(StringUtils.isNoneEmpty(endLprDate), LprConfiguration::getLprDate, endLprDate)
                .like(StringUtils.isNotEmpty(lpr.getCondition().getLprVariety()), LprConfiguration::getLprVariety, lpr.getCondition().getLprVariety())
                .ge(StringUtils.isNotEmpty(startEffectiveDate), LprConfiguration::getSystemEffectiveTime, startEffectiveDate)
                .le(StringUtils.isNotEmpty(endEffectiveDate), LprConfiguration::getSystemEffectiveTime, endEffectiveDate)
                .eq(StringUtils.isNotEmpty(lpr.getCondition().getStatus()), LprConfiguration::getStatus, lpr.getCondition().getStatus())
                .orderByDesc(LprConfiguration::getLprDate, LprConfiguration::getLprVariety)
        ));
    }

    @Transactional
    @PostMapping("/save")
    @ApiOperation(value = "保存LPR数据")
    public IResponse saveLpr(@Valid @RequestBody LprConfiguration lpr) throws ParseException {
        if (lpr.getApplicationPeriodStart() > lpr.getApplicationPeriodEnd()) {
            return IResponse.fail("应用期数起不能大于应用期数止");
        }
        if(lpr.getLprDate().compareTo(lpr.getSystemEffectiveTime())>=0){
            return IResponse.fail("生效时间不能小于等于发布时间");
        }
        if (lprService.getById(lpr.getId()) != null) {

            lprService.updateById(lpr);
        } else {
            lpr.setStatus(AfsEnumUtil.key(StatusEnum.NO));
            lprService.save(lpr);
        }
        return IResponse.success(Boolean.TRUE).setMsg("操作成功");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    @Transactional
    @SysLog("删除LPR配置信息")
    public IResponse<Boolean> delById(@PathVariable String id) {

        lprService.removeById(id);
        return new IResponse<Boolean>().setMsg("批量删除成功");
    }

    @PostMapping("/getLprNew")
    @ApiOperation(value = "根据期数查询最新LPR数据")
    public IResponse getLprNew(@RequestBody ProductPlanRate productRate) {
        Double lprRate = lprService.getLprNew(productRate.getLoanPeriodStart(),productRate.getLoanPeriodEnd());
        return new IResponse().setData(lprRate);
    }


}
